(function () {
    //帖子容器
    var cardContainer = document.getElementById('cardContainer');


    //主贴
    var userName = document.getElementById('user-name');
    var userHeadImg = document.getElementById('user-head-img');
    var cardTime = document.getElementById('card-time');
    var cardLabel = document.getElementById('card-label');
    var cardContent = document.getElementById('card-content');
    var cardImageContainer = document.getElementById('card-img-container');

    //评论
    var commentUserHeadImg = document.getElementById('comment-user-head-img');
    var commentUserName = document.getElementById('comment-user-name');
    var commentUserContent = document.getElementById('comment-user-content');
    var send = document.getElementById('send');
    var userInfo;

    var mainCard;
    var page = 0;
    var reCardList;

    initMainCard();
    initReCard();
    watchScroll();
    initListener();

    //初始化主贴
    function initMainCard() {
        var cardJson = CommonUtil.getCookie('checkCard');
        console.log(cardJson);
        mainCard = JSON.parse(cardJson);
        userName.innerHTML = mainCard.userName;
        userHeadImg.src = mainCard.headImg;
        cardTime.innerHTML = CommonUtil.timeFormat(mainCard.card.time);
        var labelArr = CommonUtil.labelFormat(mainCard.card.label);
        for (var i = 0; i < labelArr.length; i++) {
            cardLabel.insertAdjacentHTML('afterBegin', labelTemplate(labelArr[i]));
        }
        cardContent.innerHTML = mainCard.card.content;
        var imagePathArr = CommonUtil.formatImagePath(mainCard.card.img);
        for (var i = 0; i < imagePathArr.length; i++){
            cardImageContainer.insertAdjacentHTML('afterBegin', mainCardImageTemplate(imagePathArr[i]))
        }
    }

    function initListener() {
        send.addEventListener('click', inputReCard);
    }

    //提交回帖
    function inputReCard() {
        var reCard = {
            type: 0,
            replyId: mainCard.card.id,
            userId: userInfo.id,
            text: commentUserContent.value,
            img: '',
            time: Date.parse(new Date()) + '',
            receive: 0
        };
        var reCardJson = JSON.stringify(reCard);
        console.log(reCardJson);
        Api.ajax({
            method: 'POST',
            url: '/reCard',
            data: {
                reply: reCardJson
            },
            success: function (response) {
                if (response.code == 0) {
                    console.log(response);
                } else {
                    console.log(response.message);
                }
            }
        })
    }

    //初始化用户回帖
    function initReCard() {
        userInfo = CommonUtil.isLogin();
        if (userInfo == null) {
            commentUserName.innerHTML = '尚未登陆';
        } else {
            console.log(userInfo);
            commentUserName.innerHTML = userInfo.name;
            commentUserHeadImg.src = userInfo.headImg;
        }
    }

    function mainCardImageTemplate(imagePath) {
        return "<div class=\"mdui-col\">\n" +
            "<img class=\"card-img\" src=\""+ imagePath +"\"/>\n" +
            "</div>"
    }

    //标签模板
    function labelTemplate(label) {
        return "<div id='lab_first'>" + label + "</div>"
    }

    //回帖模板
    function reCardTemplate(reCard) {
        return "<div class=\"mdui-card comment\">\n" +
            "<div class=\"mdui-card-header\">\n" +
            "<img id=\"viwer-sculpture\" class=\"mdui-card-header-avatar\" src=\"" + reCard.headImg + "\"/>\n" +
            "<div id=\"viewer-name\" class=\"mdui-card-header-title\">" + reCard.userName + "</div>\n" +
            "<div id=\"view-time\" class=\"mdui-card-header-subtitle\">" + CommonUtil.timeFormat(reCard.reCard.time) +
            "</div>\n" +
            "</div>\n" +
            "<div id=\"comment\" class=\"mdui-card-content\">\n" + reCard.reCard.text +
            "</div>\n" +
            "</div>"
    }


    function addReCardList() {
        for (var i = 0; i < reCardList.length; i++) {
            cardContainer.insertAdjacentHTML("beforeEnd", reCardTemplate(reCardList[i]));
        }
    }

    //监听滑动
    function watchScroll() {
        if (!CommonUtil.isScrollToPageBottom()) {
            setTimeout(arguments.callee, 900);
            return;
        }
        getReCardList();
    }

    //获取回帖列表
    function getReCardList() {
        Api.ajax({
            method: 'POST',
            url: 'getReCardFromCard',
            data: {
                cardId: mainCard.card.id,
                page: page,
                maxPage: 10
            },
            success: function (response) {
                if (response.code == 0) {
                    console.log(response);
                    reCardList = response.data;
                    setTimeout(watchScroll, 900);
                    //TODO 显示隐藏 loading
                    // loading.style.display = 'none';
                    addReCardList();
                } else {
                    console.log(response.message);
                    // loading.innerHTML = '没有更多了';
                }
            }
        });
        page++;
    }

})();